from django.db import models


class Author(models.Model):
    name = models.CharField('author',max_length=1024)

    def __unicode__(self):
        return self.name


class Publisher(models.Model):
    name = models.CharField('publisher',max_length=200)
    def __unicode__(self):
        return self.name

class Book(models.Model):
    CONDITION_CHOICES = (
        ('0', 'Unknown'),
        ('1', 'Excellent'),
        ('2', 'Good'),
        ('3', 'OK'),
        ('4', 'Poor'),
    )

    BOOK_TYPE_CHOICES = (
        ('1', 'Hardback'),
        ('2', 'Paperback'),
        ('3', 'Leaflet'),
    )
    title = models.CharField(max_length=1024)
    description = models.TextField(blank=True)
    authors = models.ManyToManyField(Author, blank=True, related_name='author')
    isbn = models.CharField("ISBN", max_length=64,blank=True)
    image = models.ImageField(upload_to='upload/',blank=True)
    publisher = models.ForeignKey(Publisher, primary_key=False, blank=True, null=True)
    published_date = models.IntegerField(blank=True, null=True)
    cost = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    condition = models.IntegerField(choices=CONDITION_CHOICES)
    book_type = models.IntegerField(choices=BOOK_TYPE_CHOICES)
    copies = models.IntegerField()
    notes = models.TextField(blank=True, null=True)
    amazon_url = models.CharField(max_length=1024, blank=True, null=True)
    created_date = models.DateTimeField(auto_now=True, auto_now_add=True)
    def __unicode__(self):
        return self.title


